package com.ruoyi.quartz.task;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.config.ThreadExecutorPoolConfig;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.V2.inter.mapper.ApiTaskPoolMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.V2.inter.mapper.JymtaskMapper;
import com.ruoyi.system.V2.inter.mapper.SalePartnerMapper;
import com.ruoyi.system.model.domain.ApiTaskPool;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GoodsInfo;
import com.ruoyi.system.model.mapper.AccountInfoMapper;
import com.ruoyi.system.outto.service.IOutApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

@Slf4j
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
//@EnableAsync
public class EditScheduleTask {


    @Resource
    private ApiTaskPoolMapper apiTaskPoolMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private IOutApi iOutApi;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ThreadExecutorPoolConfig threadExecutorPoolConfig;

    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    private void configureTasks() {

        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());

        /**
         * 1、循环任务池信息
         * 2、调用接口任务，成功的，修改状态
         */
        ApiTaskPool apiTaskPool = new ApiTaskPool();
        apiTaskPool.setStatus(3l);
        apiTaskPool.setCountt(1);
        apiTaskPool.setSize(30l);
        apiTaskPool.setType("edit");
//        apiTaskPool.setNotPartCode("JYM");


        List<ApiTaskPool> apiTaskPoolList = apiTaskPoolMapper.selectApiTaskPoolList(apiTaskPool);

        List<Callable<Object>> tasks = new ArrayList<>();

        for (ApiTaskPool taskPool : apiTaskPoolList) {
            try {
                if(taskPool.getConfig().contains("YSY")||taskPool.getConfig().contains("THG")){
                    Thread.sleep(6000l);
                }
                GoodsInfo goodsInfo = goodsInfoMapper.selectGoodsInfoById(taskPool.getAccountId());
                if (Objects.nonNull(goodsInfo)) {
                    String goods = "edit"+goodsInfo.getId().toString()+taskPool.getConfig().toString();
                    //查询缓存是否已删除
                    if(Objects.isNull(redisCache.getCacheObject(goods))) {
                        //添加缓存
                        redisCache.setCacheObject(goods, "fangchongfu", 1, TimeUnit.MINUTES);
                        iOutApi.edit(goodsInfo.getId(), Long.parseLong(taskPool.getConfig()), null);
                    }
                } else {
                    taskPool.setStatus(0l);
                    apiTaskPoolMapper.updateApiTaskPool(taskPool);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


//        for (ApiTaskPool taskPool : apiTaskPoolList) {
//            tasks.add(() -> {
//                String type = taskPool.getType();
//                if (true) {
//                    if (Constant.edit.equals(type)) {
//                        GoodsInfo goodsInfo = goodsInfoMapper.selectGoodsInfoById(taskPool.getAccountId());
//                        if (Objects.nonNull(goodsInfo)) {
//
//                            iOutApi.edit(taskPool.getAccountId(), Long.parseLong(taskPool.getConfig()), taskPool.getId());
//                        } else {
//                            taskPool.setStatus(0l);
//                            apiTaskPoolMapper.updateApiTaskPool(taskPool);
//                        }
//                    }
//                } else {
//                    taskPool.setStatus(0l);
//                    apiTaskPoolMapper.updateApiTaskPool(taskPool);
//                }
//                return null;
//            });
//        }
//        try {
//            threadExecutorPoolConfig.threadPoolTaskExecutor().invokeAll(tasks);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }

}
